//
//  DeviceMoreViewController.m
//  upnpxdemo
//
//  Created by a  on 13-8-26.
//  Copyright (c) 2013年 wiimu.inc. All rights reserved.
//

#import "DeviceMoreViewController.h"
#import "DeviceAttributeViewController.h"
#import "DeviceSettingViewController.h"
#import "MusicDownloadViewController.h"

@interface DeviceMoreViewController ()

@end

@implementation DeviceMoreViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




- (NSString *)tabImageName
{
	return @"more.png";
}

- (NSString *)tabTitle
{
	return NSLocalizedString(@"MORE_TITLE",@"");
}

#pragma mark - navigation methods
-(NSString *)navigationBarTitle
{
    return [[GlobalInfo sharedInstance].currentSelectedUpnPInfo objectForKey:@"ssid"];
}

-(BOOL)isNewVersionButtonEnabled
{
    return YES;
}

-(IBAction)backButtonPressed:(id)sender
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"PopTabBarController" object:self];
}

#pragma mark -
#pragma mark UITableView Delegate & Datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0)
        return 1;
    else
        return 4;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }

    // Configure the cell.
    if(indexPath.section == 0)
    {
        cell.textLabel.text = NSLocalizedString(@"MUSICDOWN_CONTROL",@"");
    }
    else if(indexPath.section == 1)
    {
        switch (indexPath.row) {
            case 0:
                cell.textLabel.text = NSLocalizedString(@"DEVICE_ATTRIBUTE",@"");
                break;
            case 1:
                cell.textLabel.text = NSLocalizedString(@"DEVICE_CONFIGURE",@"");
                break;
            case 2:
                cell.textLabel.text = NSLocalizedString(@"DEVICE_RESET",@"");
                break;
            case 3:
                cell.textLabel.text = NSLocalizedString(@"DEVICE_REBOOT",@"");
                break;
            default:
                break;
        }
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
    
    NSString * iosStr = nil;
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        iosStr = @"CustomAlertView";
    } else {
        iosStr = @"CustomAlertView_iPad";
    }
    
    if(indexPath.section == 0)
    {
        MusicDownloadViewController * con = [[MusicDownloadViewController alloc] initWithNibName:@"MusicDownloadViewController" bundle:nil];
        [self.navigationController pushViewController:con animated:YES];
    }
    else if(indexPath.section == 1)
    {
        if(indexPath.row == 0)
        {
            DeviceAttributeViewController * con = [[DeviceAttributeViewController alloc] initWithNibName:@"DeviceAttributeViewController" bundle:nil];
            [self.navigationController pushViewController:con animated:YES];
        }
        else if(indexPath.row == 1)
        {
            DeviceSettingViewController * con = [[DeviceSettingViewController alloc] initWithNibName:@"DeviceSettingViewController" bundle:nil];
            [self.navigationController pushViewController:con animated:YES];
        }
        else if(indexPath.row == 2)
        {
            CustomAlertView * av = [[[NSBundle mainBundle] loadNibNamed:iosStr owner:self options:nil] lastObject];
            [av setTitle:NSLocalizedString(@"ALERT_TITLE",@"") message:NSLocalizedString(@"ALERT_DEVICERESET",@"") confirmButton:NSLocalizedString(@"CONFIRM",@"") cancelButton:NSLocalizedString(@"CANCEL",@"") delegate:self];
            [self.view addSubview:av];
        }
        else if(indexPath.row == 3)
        {
            CustomAlertView * av = [[[NSBundle mainBundle] loadNibNamed:iosStr owner:self options:nil] lastObject];
            [av setTitle:NSLocalizedString(@"ALERT_TITLE",@"") message:NSLocalizedString(@"ALERT_DEVICEREBOOT",@"") confirmButton:NSLocalizedString(@"CONFIRM",@"") cancelButton:NSLocalizedString(@"CANCEL",@"") delegate:self];
            [self.view addSubview:av];
        }
    }
}

#pragma mark -
#pragma mark servicecallbacks
-(void)serviceResetDeviceCallBack:(NSDictionary*)dicCallBack
{
    
}

-(void)serviceRebootDeviceCallBack:(NSDictionary*)dicCallBack
{

}

#pragma mark -
#pragma mark CustomAlertView callback
-(void)CustomAlertView:(CustomAlertView *)alertView selected:(BOOL)confirmed
{
    if([alertView.messageLabel.text isEqualToString:NSLocalizedString(@"ALERT_DEVICERESET",@"")])
    {
        if(confirmed == YES)//确定
        {
            [[GlobalService sharedInstance] resetDevice:self];
            
            [self.view makeToast:NSLocalizedString(@"ALERT_RESETING",@"")
                        duration:3.0
                        position:@"bottom"
                           title:NSLocalizedString(@"ALERT_TITLE",@"")];
        }
    }
    else if([alertView.messageLabel.text isEqualToString:NSLocalizedString(@"ALERT_DEVICEREBOOT",@"")])
    {
        if(confirmed == YES)//确定
        {
            [[GlobalService sharedInstance] rebootDevice:self];
            
            [self.view makeToast:NSLocalizedString(@"ALERT_REBOOTTING",@"")
                        duration:3.0
                        position:@"bottom"
                           title:NSLocalizedString(@"ALERT_TITLE",@"")];
        }
    }
}
@end
